//
//  KKSandClock.m
//  boboVideo
//
//  Created by apple on 2021/5/26.
//

#import "KKSandClock.h"
#import "SandClockLayer.h"
#import <Masonry/Masonry.h>
#import "HeaderConfig.h"

@interface KKSandClock ()
@property(nonatomic, strong) SandClockLayer *clockLayer;
@property(nonatomic, assign, readwrite) BOOL isAnimationIng;

@end

@implementation KKSandClock

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUI];
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI {
    self.backgroundColor = [UIColor clearColor];

    NSBundle *containnerBundle = [NSBundle bundleForClass:[self class]];
    NSBundle *resourceBundle = [NSBundle bundleWithPath:[containnerBundle pathForResource:@"oneCommonLib" ofType:@"bundle"]];
    // 固定48，56
    UIImage *bgImage = [UIImage imageNamed:@"sandclock_bg" inBundle:resourceBundle compatibleWithTraitCollection:nil];
    self.clockLayer = [[SandClockLayer alloc] initWithFrame:CGRectMake(0, 0, bgImage.size.width / 2, bgImage.size.height / 2)];
    [self.layer addSublayer:self.clockLayer];
    self.clockLayer.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);

    self.subTitleLB = [[UILabel alloc] initWithFrame:CGRectZero];
    self.subTitleLB.text = @"";
    self.subTitleLB.font = PingFangBold(tRealLength(16));
    self.subTitleLB.textColor = [UIColor whiteColor];
    self.subTitleLB.numberOfLines = 0;
    self.subTitleLB.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.subTitleLB];

    //    [self.clockLayer startAnimation];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.clockLayer.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);

    [self.subTitleLB mas_makeConstraints:^(MASConstraintMaker *make) {
      make.centerX.equalTo(self);
      make.centerY.equalTo(self).offset(70);
      make.width.mas_equalTo(self).multipliedBy(0.6);
    }];
}

- (void)startAnimation {
    if (self.isAnimationIng == YES) return;
    [self.clockLayer startAnimation];
    self.isAnimationIng = YES;
}

- (void)stopAnimation {
    if (self.isAnimationIng == NO) return;

    self.isAnimationIng = NO;
    [self.clockLayer stopAnimation];
}

@end
